package account

import (
  "fmt"
  "gitee.com/yizhisamoye/GoWeChat/entity/common"
  "gitee.com/yizhisamoye/GoWeChat/utils"
)
/**
群发
 */

//群发，同素材管理中的上传临时图文消息一样
func(w WxAccount)SendNews(news []common.News)(*common.Material,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/media/uploadnews?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]interface{}{
    "articles":news,
  })
  if err!=nil{
    return nil,err
  }
  m:=new(common.Material)
  m.Type=result["type"].(string)
  m.MediaId=result["media_id"].(string)
  m.CreateAt=int(result["create_at"].(float64))
  return m,nil
}

//根据标签群发消息
func(w WxAccount)sendMsgByTag(tagId int,msgType string,data map[string]interface{})(map[string]int,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/mass/sendall?access_token=%s",w.accessToken)
  data["filter"]=map[string]interface{}{
    "is_to_all":false,
    "tag_id":tagId,
  }
  result,err:=utils.SendJson(url,data)
  if err!=nil{
    return nil,err
  }
  m:=make(map[string]int)
  m["msgId"]=int(result["msg_id"].(float64))
  if v,exist:=result["msg_data_id"];exist{
    m["msgDataId"]=int(v.(float64))
  }
  return m,nil
}
//根据标签群发图文消息
func(w WxAccount)SendNewsByTag(tagId int,mediaId string,sendIgnoreReprint bool)(msgId,msgDataId int,err error){
  flag:=0
  if sendIgnoreReprint{
    flag=1
  }
  data:=map[string]interface{}{
    "mpnews":map[string]string{
      "media_id":mediaId,
    },
    "send_ignore_reprint":flag,
  }
  m,err:=w.sendMsgByTag(tagId,"mpnews",data)
  if err!=nil{
    return 0,0,err
  }
  return m["msgId"],m["msgDataId"],nil
}
//根据标签群发文字消息
func(w WxAccount)SendTextByTag(tagId int,content string)(msgId int,err error){
  data:=map[string]interface{}{
    "text":map[string]string{
      "content":content,
    },
  }
  m,err:=w.sendMsgByTag(tagId,"text",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据标签群发语音消息
func(w WxAccount)SendVoiceByTag(tagId int,mediaId string)(msgId int,err error){
  data:=map[string]interface{}{
    "voice":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.sendMsgByTag(tagId,"voice",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据标签群发图片消息
func(w WxAccount)SendImageByTag(tagId int,mediaIds []string,recommend string,needOpenComment,onlyFansCanComment bool)(msgId int,err error){
  commentFlag:=0
  fanComment:=0
  if needOpenComment{
    commentFlag=1
  }
  if needOpenComment&&onlyFansCanComment{
    fanComment=1
  }
  data:=map[string]interface{}{
    "images":map[string]interface{}{
      "media_ids":mediaIds,
    },
    "recommend":recommend,
    "need_open_comment":commentFlag,
    "only_fans_can_comment":fanComment,
  }
  m,err:=w.sendMsgByTag(tagId,"image",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据标签群发视频消息
func(w WxAccount)SendVideoByTag(tagId int,mediaId string)(msgId int,err error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/media/uploadvideo?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]string{
    "media_id":mediaId,
    "title":"video",
    "description":"video",
  })
  if err!=nil{
    return 0,err
  }
  mediaId=result["media_id"].(string)
  data:=map[string]interface{}{
    "mpvideo":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.sendMsgByTag(tagId,"mpvideo",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据标签群发卡券消息
func(w WxAccount)SendCardByTag(tagId int,cardId string)(msgId int,err error){
  data:=map[string]interface{}{
    "wxcard":map[string]string{
      "card_id":cardId,
    },
  }
  m,err:=w.sendMsgByTag(tagId,"wxcard",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}

//根据openid发消息
func(w WxAccount)sendMsgByOpenId(toUserOpenIds []string,msgType string,data map[string]interface{})(map[string]int,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/mass/send?access_token=%s",w.accessToken)
  data["touser"]=toUserOpenIds
  result,err:=utils.SendJson(url,data)
  if err!=nil{
    return nil,err
  }
  m:=make(map[string]int)
  m["msgId"]=int(result["msg_id"].(float64))
  if v,exist:=result["msg_data_id"];exist{
    m["msgDataId"]=int(v.(float64))
  }
  return m,nil
}
//根据openid群发图文消息
func(w WxAccount)SendNewsByOpenId(toUserOpenIds []string,mediaId string,sendIgnoreReprint bool)(msgId,msgDataId int,err error){
  flag:=0
  if sendIgnoreReprint{
    flag=1
  }
  data:=map[string]interface{}{
    "mpnews":map[string]string{
      "media_id":mediaId,
    },
    "send_ignore_reprint":flag,
  }
  m,err:=w.sendMsgByOpenId(toUserOpenIds,"mpnews",data)
  if err!=nil{
    return 0,0,err
  }
  return m["msgId"],m["msgDataId"],nil
}
//根据openid群发文字消息
func(w WxAccount)SendTextByOpenId(toUserOpenIds []string,content string)(msgId int,err error){
  data:=map[string]interface{}{
    "text":map[string]string{
      "content":content,
    },
  }
  m,err:=w.sendMsgByOpenId(toUserOpenIds,"text",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据openid群发语音消息
func(w WxAccount)SendVoiceByOpenId(toUserOpenIds []string,mediaId string)(msgId int,err error){
  data:=map[string]interface{}{
    "voice":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.sendMsgByOpenId(toUserOpenIds,"voice",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据openid群发图片消息
func(w WxAccount)SendImageByOpenId(toUserOpenIds []string,mediaIds []string,recommend string,needOpenComment,onlyFansCanComment bool)(msgId int,err error){
  commentFlag:=0
  fanComment:=0
  if needOpenComment{
    commentFlag=1
  }
  if needOpenComment&&onlyFansCanComment{
    fanComment=1
  }
  data:=map[string]interface{}{
    "images":map[string]interface{}{
      "media_ids":mediaIds,
    },
    "recommend":recommend,
    "need_open_comment":commentFlag,
    "only_fans_can_comment":fanComment,
  }
  m,err:=w.sendMsgByOpenId(toUserOpenIds,"image",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据openid群发视频消息
func(w WxAccount)SendVideoByOpenId(toUserOpenIds []string,mediaId string)(msgId int,err error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/media/uploadvideo?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]string{
    "media_id":mediaId,
    "title":"video",
    "description":"video",
  })
  if err!=nil{
    return 0,err
  }
  mediaId=result["media_id"].(string)
  data:=map[string]interface{}{
    "mpvideo":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.sendMsgByOpenId(toUserOpenIds,"mpvideo",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}
//根据openid群发卡券消息
func(w WxAccount)SendCardByOpenId(toUserOpenIds []string,cardId string)(msgId int,err error){
  data:=map[string]interface{}{
    "wxcard":map[string]string{
      "card_id":cardId,
    },
  }
  m,err:=w.sendMsgByOpenId(toUserOpenIds,"wxcard",data)
  if err!=nil{
    return 0,err
  }
  return m["msgId"],nil
}

//删除群发
func(w WxAccount)DeleteMsg(msgId string,articleIndex ...int)error{
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/mass/delete?access_token=%s",w.accessToken)
  deleteArticleIndex:=0
  if len(articleIndex)>0{
    deleteArticleIndex=articleIndex[0]
  }
  _,err:=utils.SendJson(url,map[string]interface{}{
    "msg_id":msgId,
    "article_idx":deleteArticleIndex,
  })
  return err
}

//预览
func(w WxAccount)previewMsg(toUserOpenId string,msgType string,data map[string]interface{},toWxName ...string)(int,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/mass/preview?access_token=%s",w.accessToken)
  if len(toWxName)>0{
    data["toWxName"]=toWxName[0]
  }
  data["touser"]=toUserOpenId
  data["msgtype"]=msgType
  result,err:=utils.SendJson(url,data)
  if err!=nil{
    return 0,err
  }
  if v,exist:=result["msg"];exist{
    return int(v.(float64)),err
  }else{
    return 0,nil
  }
}
//预览图文消息
func(w WxAccount)PreviewNews(toUserOpenId string,mediaId string,toWxName ...string)(msgId int,err error){
  data:=map[string]interface{}{
    "mpnews":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.previewMsg(toUserOpenId,"mpnews",data,toWxName...)
  if err!=nil{
    return 0,err
  }
  return m,nil
}
//预览文字消息
func(w WxAccount)PreviewText(toUserOpenId string,content string,toWxName ...string)(msgId int,err error){
  data:=map[string]interface{}{
    "text":map[string]string{
      "content":content,
    },
  }
  m,err:=w.previewMsg(toUserOpenId,"text",data,toWxName...)
  if err!=nil{
    return 0,err
  }
  return m,nil
}
//预览语音消息
func(w WxAccount)PreviewVoice(toUserOpenId string,mediaId string,toWxName ...string)(msgId int,err error){
  data:=map[string]interface{}{
    "voice":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.previewMsg(toUserOpenId,"voice",data,toWxName...)
  if err!=nil{
    return 0,err
  }
  return m,nil
}
//预览图片消息
func(w WxAccount)PreviewImage(toUserOpenId string,mediaIds []string,recommend string,toWxName ...string)(msgId int,err error){
  data:=map[string]interface{}{
    "images":map[string]interface{}{
      "media_ids":mediaIds,
    },
    "recommend":recommend,
  }
  m,err:=w.previewMsg(toUserOpenId,"image",data,toWxName...)
  if err!=nil{
    return 0,err
  }
  return m,nil
}
//预览视频消息
func(w WxAccount)PreviewVideo(toUserOpenId string,mediaId string,toWxName ...string)(msgId int,err error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/media/uploadvideo?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]string{
    "media_id":mediaId,
    "title":"video",
    "description":"video",
  })
  if err!=nil{
    return 0,err
  }
  mediaId=result["media_id"].(string)
  data:=map[string]interface{}{
    "mpvideo":map[string]string{
      "media_id":mediaId,
    },
  }
  m,err:=w.previewMsg(toUserOpenId,"mpvideo",data,toWxName...)
  if err!=nil{
    return 0,err
  }
  return m,nil
}
//预览发卡券消息
func(w WxAccount)PreviewCard(toUserOpenId string,cardId string,toWxName ...string)(msgId int,err error){
  data:=map[string]interface{}{
    "wxcard":map[string]string{
      "card_id":cardId,
    },
  }
  m,err:=w.previewMsg(toUserOpenId,"wxcard",data,toWxName...)
  if err!=nil{
    return 0,err
  }
  return m,nil
}
//查询消息群发状态
func(w WxAccount)GetMsgStatus(msgId string)(string,error){
  url:=fmt.Sprintf(BaseUrl+"/cgi-bin/message/mass/get?access_token=%s",w.accessToken)
  result,err:=utils.SendJson(url,map[string]string{
    "msg_id":msgId,
  })
  if err!=nil{
    return "",err
  }
  return result["msg_status"].(string),nil
}
